package com.atguigu.ggkt.wechat.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.atguigu.ggkt.exception.CustomException;
import com.atguigu.ggkt.model.wechat.Menu;
import com.atguigu.ggkt.vo.wechat.MenuVo;
import com.atguigu.ggkt.wechat.mapper.MenuMapper;
import com.atguigu.ggkt.wechat.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author 杰哥
 * @since 2022-08-02
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Resource
    private WxMpService wxMpService;

    //获取所有菜单，按照一级和二级菜单封装
    @Override
    public List<MenuVo> findMenuInfo() {
        //创建集合用作用户最终数据封装
        List<MenuVo> finalMenuVoList = new LinkedList<>();
        //查询所有菜单数据（包含一级和二级）
        List<Menu> menuList = baseMapper.selectList(null);
        //从所有菜单数据获取所有的一级菜单数据
        List<Menu> oneMenuList = menuList.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
        for (Menu oneMenu : oneMenuList) {
            //menu --> menuVo
            MenuVo oneMenuVo = new MenuVo();
            BeanUtils.copyProperties(oneMenu, oneMenuVo);
            //封装二级菜单数据（判断一级菜单id和二级菜单的parent_id是否相同）
            //如果相同，则把二级菜单数据放到一级菜单里面
            List<Menu> twoMenuList = menuList.stream().filter(menu -> menu.getParentId() == oneMenu.getId()).collect(Collectors.toList());
            //List<Menu> --> List<MenuVo>
            List<MenuVo> children = new LinkedList<>();
            for (Menu twoMenu : twoMenuList) {
                MenuVo twoMenuVo = new MenuVo();
                BeanUtils.copyProperties(twoMenu, twoMenuVo);
                children.add(twoMenuVo);
            }
            //把二级菜单数据放到一级菜单里面
            oneMenuVo.setChildren(children);
            //把oneMenuVo放到最终的集合中去
            finalMenuVoList.add(oneMenuVo);
        }
        return finalMenuVoList;
    }
    //获取所有一级菜单
    @Override
    public List<Menu> findMenuOneInfo() {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", 0);
        List<Menu> menuList = baseMapper.selectList(wrapper);
        return menuList;
    }

    //同步菜单
    @Override
    public void syncMenu() {
        //获取所有菜单数据
        List<MenuVo> menuVoList = findMenuInfo();
        //封装button里面的结构，数组格式
        JSONArray buttonList = new JSONArray();
        for (MenuVo oneMenuVo : menuVoList) {
            //json对象 一级菜单
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            //json数组 二级菜单
            JSONArray subButton = new JSONArray();
            for (MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                JSONObject view = new JSONObject();
                view.put("type", twoMenuVo.getType());
                if(twoMenuVo.getType().equals("view")) {
                    view.put("name", twoMenuVo.getName());
                    view.put("url", "http://zxkt2.free.idcfengye.com/#"
                            +twoMenuVo.getUrl());
                } else {
                    view.put("name", twoMenuVo.getName());
                    view.put("key", twoMenuVo.getMeunKey());
                }
                subButton.add(view);
            }
            one.put("sub_button", subButton);
            buttonList.add(one);
        }
        //封装最外层的button部分
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        System.out.println("！！！！封装最外层的button部分\n" + button);
        try {
            String menuId = wxMpService.getMenuService().menuCreate(button.toString());
            System.out.println("menuId=" + menuId);
        } catch (WxErrorException e) {
            System.out.println("微信公众号同步菜单失败！");
            throw new CustomException(20001, "微信公众号同步菜单失败！" + e);
        }
    }
    //公众号菜单删除
    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new CustomException(20001, "微信公众号菜单删除失败！");
        }
    }
}
